﻿unit SysDeptUserService;

interface
{$I ../Config.inc}
uses
  FastMM4,
  FastMove,
  System.SysUtils,
  System.Classes,
  System.Types,
  Winapi.Windows,
  Data.DB,
  Data.Win.ADODB,
  Datasnap.DBClient,
  Datasnap.Provider,
  {$IFDEF UNICODE}
  AnsiStrings,
  {$ENDIF }
  QBParcel,
  DllSpread,
  Generics.Collections,
  DateUtils,
  QJson,
  UserConnection,
  DBAccessor,
  DllPlugin,
  StrUtils,
  SKSOFTComm,
  SysDeptUserModel,
  SysDeptUserBLL,
  SysDepartmentModel,
  SysUserModel;
  
type

  TSysDeptUserService = class(TSysDeptUserBLL)
  private

  protected

  public
  {$IFDEF SERVER}
    function BeforeTreatment(const iItem:TSysDeptUser; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function BeforeTreatment(const iItems:TSysDeptUserList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;

    function BeforeCheck(const iItem:TSysDeptUser; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function BeforeCheck(const iItems:TSysDeptUserList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;

    function Check(const iItem:TSysDeptUser; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function Check(const iItems:TSysDeptUserList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;

    function BeforeExecute(const iItem:TSysDeptUser; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function BeforeExecute(const iItems:TSysDeptUserList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;

    function AfterTreatment(const iItem:TSysDeptUser; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function AfterTreatment(const iItems:TSysDeptUserList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
  {$ENDIF}

    function LoadAll(const iOrgId:string; const iItems:TSysDeptUserList; out oError:string; const iJustUseDept:Boolean=False; const iJustUseUser:Boolean=False):Boolean;
    function LoadByDeptId(const iDeptId:string; const iItems:TSysDeptUserList; out oError:string; const iJustUseUser:Boolean=False):Boolean;
    function LoadByUserId(const iUserId,iOrgId:string; const iItems:TSysDeptUserList; out oError:string; const iJustUseDept:Boolean=False):Boolean;
  end;
  
implementation

{$IFDEF SERVER}
function TSysDeptUserService.BeforeTreatment(const iItem:TSysDeptUser; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TSysDeptUserService.BeforeTreatment(const iItems:TSysDeptUserList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TSysDeptUserService.BeforeCheck(const iItem:TSysDeptUser; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TSysDeptUserService.BeforeCheck(const iItems:TSysDeptUserList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TSysDeptUserService.Check(const iItem:TSysDeptUser; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TSysDeptUserService.Check(const iItems:TSysDeptUserList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TSysDeptUserService.BeforeExecute(const iItem:TSysDeptUser; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TSysDeptUserService.BeforeExecute(const iItems:TSysDeptUserList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TSysDeptUserService.AfterTreatment(const iItem:TSysDeptUser; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TSysDeptUserService.AfterTreatment(const iItems:TSysDeptUserList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;
{$ENDIF}

function TSysDeptUserService.LoadAll(const iOrgId:string; const iItems:TSysDeptUserList; out oError:string; const iJustUseDept:Boolean=False; const iJustUseUser:Boolean=False):Boolean;
var
  Condition,OrderBy:string;
begin
  if (not SameStr(iOrgId,EmptyStr)) and (iItems<>nil) then
  begin
    Condition:=Format(string('%s=''%s'' '),[TSysDepartmentModel.cOrgIdKey,iOrgId]);
    if iJustUseDept then
    begin
      Condition:=Condition+Format(string(' and %s=1'),[TSysDepartmentModel.cDeptStatusKey]);
    end;
    if iJustUseUser then
    begin
      Condition:=Condition+Format(string(' and %s=1'),[TSysUserModel.cUserStatusKey]);
    end;
    OrderBy:=TSysUserModel.cUserIdKey+' ASC';

    if not LoadByCondition(iItems,Condition,oError,OrderBy,eDBLoadMode.dblmView) then
    begin
      Result:=False;
      Exit;
    end;
  end else begin
    oError:=Format(string('调用TSysDeptUserService.LoadAll方法失败！错误内容:传入的iOrgId或iItems为空!'),[]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TSysDeptUserService.LoadByDeptId(const iDeptId:string; const iItems:TSysDeptUserList; out oError:string; const iJustUseUser:Boolean=False):Boolean;
var
  Condition,OrderBy:string;
begin
  if (not SameStr(iDeptId,EmptyStr)) and (iItems<>nil) then
  begin
    if iJustUseUser then
    begin
      Condition:=Format(string('%s=''%s'' and %s=1'),[TSysDeptUserModel.cDeptIdKey,iDeptId,TSysUserModel.cUserStatusKey]);
    end else begin
      Condition:=Format(string('%s=''%s'''),[TSysDeptUserModel.cDeptIdKey,iDeptId]);
    end;
    OrderBy:=TSysUserModel.cUserIdKey+' ASC';

    if not LoadByCondition(iItems,Condition,oError,OrderBy,dblmView) then
    begin
      Result:=False;
      Exit;
    end;
  end else begin
    oError:=Format(string('调用TSysDeptUserService.LoadAll方法失败！错误内容:传入的iDeptId或iItems为空!'),[]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TSysDeptUserService.LoadByUserId(const iUserId,iOrgId:string; const iItems:TSysDeptUserList; out oError:string; const iJustUseDept:Boolean):Boolean;
var
  Condition,OrderBy:string;
begin
  if (not SameStr(iUserId,EmptyStr))
    and (not SameStr(iOrgId,EmptyStr))
    and (iItems<>nil) then
  begin
    Condition:=Format(string('%s=''%s'' and %s=''%s'' '),
                                        [TSysDeptUserModel.cUserIdKey,iUserId,
                                        TSysDepartmentModel.cOrgIdKey,iOrgId]);
    if iJustUseDept then
    begin
      Condition:=Condition+Format(string(' and %s=1 '),[TSysDepartmentModel.cDeptStatusKey]);
    end;
    OrderBy:=TSysDeptUserModel.cDeptIdKey+' ASC';

    if not LoadByCondition(iItems,Condition,oError,OrderBy,eDBLoadMode.dblmView) then
    begin
      Result:=False;
      Exit;
    end;
  end else begin
    oError:=Format(string('调用TSysDeptUserService.LoadByUserId方法失败！错误内容:传入的iUserId或iOrgId或iItems为空!'),[]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;
  
end.
